/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetch2.provider;

import com.tonyodev.fetch2.Download;
import com.tonyodev.fetch2.PrioritySort;
import com.tonyodev.fetch2.Status;
import com.tonyodev.fetch2.database.DownloadInfo;
import com.tonyodev.fetch2.database.FetchDatabaseManagerWrapper;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * description DownloadProvider
 *
 * @author liuluming
 * @since 2021-05-25
 */
public class DownloadProvider {
    private FetchDatabaseManagerWrapper fetchDatabaseManagerWrapper;

    public List<DownloadInfo> getDownloads() {
        return this.fetchDatabaseManagerWrapper.get();
    }

    @Nullable
    public Download getDownload(int id) {
        return (Download) this.fetchDatabaseManagerWrapper.get(id);
    }

    @NotNull
    public List<DownloadInfo> getDownloads(@NotNull List<Integer> ids) {
        return fetchDatabaseManagerWrapper.get(ids);
    }


    @NotNull
    public List<DownloadInfo> getByGroup(int group) {
        return this.fetchDatabaseManagerWrapper.getByGroup(group);
    }

    /**
     * getByGroupReplace
     *
     * @param group 1
     * @param download download
     * @return true
     */
    @NotNull
    public List<DownloadInfo> getByGroupReplace(int group, @NotNull DownloadInfo download) {
        List<DownloadInfo> list = getByGroup(group);
        ArrayList<DownloadInfo> downloads = (ArrayList<DownloadInfo>) list;
        List<DownloadInfo> indexOfList = downloads;
        int indexIv = 0;
        Iterator<DownloadInfo> iterator = indexOfList.iterator();
        int var;
        while (true) {
            if (!iterator.hasNext()) {
                var = -1;
                break;
            }
            DownloadInfo it = iterator.next();
            if (it.getId() == download.getId()) {
                var = indexIv;
                break;
            }

            ++indexIv;
        }

        int index = var;
        if (index != -1) {
            downloads.set(index, download);
        }

        return downloads;
    }


    @NotNull
    public List<? extends Download> getByStatus(@NotNull Status status) {
        return this.fetchDatabaseManagerWrapper.getByStatus(status);
    }

    @NotNull
    public List<? extends Download> getPendingDownloadsSorted(@NotNull PrioritySort prioritySort) {
        return this.fetchDatabaseManagerWrapper.getPendingDownloadsSorted(prioritySort);
    }

    public DownloadProvider(@NotNull FetchDatabaseManagerWrapper fetchDatabaseManagerWrapper) {
        super();
        this.fetchDatabaseManagerWrapper = fetchDatabaseManagerWrapper;
    }
}
